-- ==================== telescope ====================
local function set_picker_config(picker_name)
  local config = {
    theme = "ivy",
    layout_config = {
      vertical = {
        mirror = false,
      },
      height = 0.4,
    },
    -- borderchars = { " ", " ", " ", " ", "┌", "┐", "┘", "└" },
    borderchars = { " ", " ", " ", " ", " ", " ", " ", " " },
  }

  -- 针对 lsp_references 的特定配置
  if picker_name == "lsp_references" then
    config.file_ignore_patterns = { ".git/", "node_modules/" }
    config.entry_maker = function(entry)
      local filename = entry.filename or vim.api.nvim_buf_get_name(entry.bufnr)
      return {
        value = entry,
        display = function(entry_maker)
          local display_filename = vim.fn.fnamemodify(filename, ":t")
          local icon = ""
          return icon .. " " .. display_filename .. ":" .. entry.lnum .. ":" .. entry.col .. ": " .. entry.text,
              { { { 0, #icon + 1 }, "TelescopeResultsIconColor" } }
        end,
        ordinal = entry.text,
        filename = filename,
        lnum = entry.lnum,
        col = entry.col,
        text = entry.text,
      }
    end
  end
  return config
end

local telescope = require("telescope")
telescope.setup {
  pickers = {
    find_files = set_picker_config("find_files"),
    oldfiles = set_picker_config("oldfiles"),
    lsp_references = set_picker_config("lsp_references"),
    diagnostics = set_picker_config("oldfiles"),
    buffers = {
      theme = "ivy",
      layout_config = {
        vertical = {
          mirror = false,
        },
        height = 0.4,
        preview_cutoff = 200,
      },
      -- borderchars = { "─", " ", "─", " ", "─", "─", "─", "─" },
      borderchars = { " ", " ", " ", " ", " ", " ", " ", " " },
    },
  },
  defaults = {
    color_devicons = true,
    vimgrep_arguments = {
      "rg",
      "--color=never",
      "--no-heading",
      "--with-filename",
      "--line-number",
      "--column",
      "--smart-case",
    },
    prompt_prefix = " ",
    selection_caret = "» ",
    entry_prefix = "  ",
    sorting_strategy = "ascending",
    layout_config = {
      horizontal = {
        prompt_position = "top",
        preview_width = 0.55,
        results_width = 0.8,
      },
      vertical = {
        mirror = false,
      },
      width = 0.85,
      height = 0.85,
      preview_cutoff = 80,
    },
    borderchars = { "─", "│", "─", "│", "┌", "┐", "┘", "└" },
  },
}

-- 设置紫色图标的高亮组
vim.api.nvim_set_hl(0, "TelescopeResultsIconColor", { link = "Keyword", bold = true })

-- 剪切板历史
require('neoclip').setup({
  history = 100,
  enable_persistent_history = true,
  on_paste = {
    set_reg = false,  -- 设置为 false，这样在选择时不会自动设置寄存器
  },
  keys = {
    telescope = {
      i = {
        select = '<cr>',  -- 在插入模式下使用回车键选择并粘贴
        paste = '<cr>',   -- 在插入模式下使用回车键粘贴
      },
      n = {
        select = '<cr>',  -- 在普通模式下使用回车键选择并粘贴
        paste = '<cr>',   -- 在普通模式下使用回车键粘贴
      },
    },
  },
})

-- 加载 telescope-neoclip 扩展
telescope.load_extension('neoclip')
